package com.vhuomai.system.controller;

import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Scope;
import com.vhuomai.system.constant.UserRole;
import com.vhuomai.system.entity.BaseSysShare;
import com.vhuomai.system.service.ShareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author harris
 */
@Api(tags = "分享设置")
@RestController
@RequestMapping("/api")
public class ShareController {
    
    @Autowired
    private ShareService shareService;
    
    @ApiOperation(value = "页面分享信息")
    @Scope(role = UserRole.USER)
    @GetMapping(value = "/page/share/detail", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseSysShare> detail(@PathVariable("page") String page) {
        return ServiceResult.success(shareService.findByPage(page));
    }
    
    @ApiOperation(value = "分享列表")
    @Scope(role = UserRole.MANAGER)
    @GetMapping(value = "/page/shares", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<List<BaseSysShare>> shares() {
        return ServiceResult.success(shareService.findAll());
    }
    
    @ApiOperation(value = "添加分享")
    @Scope(role = UserRole.MANAGER)
    @PostMapping(value = "/page/share", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<BaseSysShare> addShare(@RequestBody BaseSysShare share) {
        return ServiceResult.success(shareService.add(share));
    }
    
    @ApiOperation(value = "修改分享")
    @Scope(role = UserRole.MANAGER)
    @PutMapping(value = "/page/share", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult<Boolean> updateShare(@RequestBody BaseSysShare share) {
        shareService.update(share);
        return ServiceResult.SUCCESS;
    }
}
